package com.dp.service.impl;
import javax.persistence.Query;

import org.springframework.stereotype.Service;

import com.dp.service.impl3.BaseImpl;
import com.dp.vo.TSpcode;

@Service
public class SpcodeServiceImpl extends BaseImpl {
    

   /* @SuppressWarnings("unchecked")
    public List<TSpcode> findAll() {
    	List<TSpcode> s=new ArrayList<TSpcode>();
    	try {
    	this.initEM();
    	
			
		       
        Query query = getEntityManager().createQuery("select p FROM TSpcode p");
       // em.createNativeQuery(arg0, arg1)
       // em.close();
        s=query.getResultList();
        
    	} catch (Exception e) {
			// TODO: handle exception
		} finally{
    		this.closeAll();
    	}   
        return s;
    }*/
    
    public boolean updateSPcode(TSpcode tspcode,String del) {
    	boolean t=true;
    	try {
//    	this.init();
    	
    	        sql="update  t_spcode set spcode=?,spname=?,spaddress=?,sptelphone=?,splinkman=?,spwebaddress=? where spcode=?";
               Query	query=em.createNativeQuery(sql);
               query.setParameter(1, tspcode.getSpcode());
               query.setParameter(2, tspcode.getSpname());
               query.setParameter(3, tspcode.getSpaddress());
               query.setParameter(4, tspcode.getSptelphone());
               query.setParameter(5, tspcode.getSplinkman());
               query.setParameter(6, tspcode.getSpwebaddress());
               query.setParameter(7, del);
			   query.executeUpdate();
					
					
					
//				this.prep=this.conn.prepareStatement(sql);
//				prep.setString(1, tspcode.getSpcode());
//				prep.setString(2, tspcode.getSpname());
//				prep.setString(3, tspcode.getSpaddress());
//				prep.setString(4, tspcode.getSptelphone());
//				prep.setString(5, tspcode.getSplinkman());
//				prep.setString(6, tspcode.getSpwebaddress());
//				prep.setString(7, del);
//				prep.executeUpdate();
				t=true;
		
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			t=false;
		}
//    	finally{this.closeAll(); }
        return t;
    }
    public boolean deleteSPcode(String del) {
    	boolean t=true;
    	try {
    	      
			 sql="delete from  t_spcode where spcode='"+del+"'";
				this.em.createNativeQuery(sql).executeUpdate();
				t=true;
		
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			t=false;
		}
    
        return t;
    }
    
    public boolean doAddNew(String... s) {
    	boolean t=true;
    	try {
    
			sql=("select count(spcode) as totalrow from t_spcode where spcode='"+s[0] +"'");
			//rs1.next();
			
			if(this.getOneInt()>0) 
			{
				
				t= false;
			}
			else
			{
				sql=("select key_value from main_key where key_name='t_source'");
				int nowkey=this.getOneInt();
				//while(rs1.next()) nowkey=Integer.parseInt(rs1.getString(1));
				String temp="0123456789abcdefghijklmnopqrstuvwxyz";
				
				int f=nowkey/temp.length();
				int l=nowkey % temp.length();				
				String sourceid=temp.substring(f,f+1)+temp.substring(l,l+1);
				sql="insert into t_spcode(spcode,spname,spaddress,sptelphone,splinkman,spwebaddress,sourceid)values(?,?,?,?,?,?,?)";
//				this.prep=this.conn.prepareStatement(sql);
				  Query	query=em.createNativeQuery(sql);
				  for(int i=0;i<6;i++)
	               query.setParameter(i+1, s[i]);
				  query.setParameter(7, sourceid);
				   query.executeUpdate();
//				prep.setString(1, s[0]);
//				prep.setString(2, s[1]);
//				prep.setString(3, s[2]);
//				prep.setString(4, s[3]);
//				prep.setString(5, s[4]);
//				prep.setString(6, s[5]);	
//				prep.setString(7, sourceid);
//				prep.executeUpdate();
			
				sql=("update  main_key set key_value=key_value+1 where key_name='t_source'");
				this.em.createNativeQuery(sql).executeUpdate();
				//dp.executeUpdate();
				//dp.close();	
				//this.addActionMessage("增加成功！");
				t=true;
				
			}	     
    
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			t=false;
		}
//    	finally{this.closeAll(); }
        return t;
    }
    
    
   /* public List<TSysAutoactivetimer> findAllTimer() {
    	List<TSysAutoactivetimer> t=null;
    	try {
    		this.initEM();
    	em.getTransaction().begin();
    	em.clear();
        Query query = getEntityManager().createQuery("select p FROM TSysAutoactivetimer p");       
        t=query.getResultList(); 
        System.out.println("dgdfgdfg");
        em.flush();
        em.getTransaction().commit();
        
    	//em.setFlushMode(FlushModeType.)
        t=new ArrayList<TSysAutoactivetimer>();
        TSysAutoactivetimer temp=null;
    	
			this.init();
		
    	this.sql="select t.timerid,t.timertime,t.timermsg,t.timequeue,t.isvalid from t_sys_autoactivetimer t" ;
    	this.rs1=stmt.executeQuery(sql);
    	while(rs1.next())
    	{
    	   temp=new TSysAutoactivetimer();
    	   temp.setTimerid(rs1.getString(1));
    	   temp.setTimertime(rs1.getString(2));
    	   temp.setTimermsg(rs1.getString(3));
    	   temp.setIsvalid(rs1.getString(5));  
    	   t.add(temp);
    	}    	
        
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally{this.closeAll(); }
        return t;
    }*/
   /* public List<TTjIncomingbyarea> findIA(String year) {
    	List<TTjIncomingbyarea> t=new ArrayList<TTjIncomingbyarea>();
    	TTjIncomingbyarea temp=null;
    	try {
			this.init();
		
    	this.sql="select upareaid,upareaname,pricetype,cmonth,sum(incoming) as incoming from t_tj_incomingbyarea a" +
    			" where a.cyear='"+year+"' group by upareaid,upareaname,pricetype,cmonth " +
    			" order by  upareaid,upareaname,pricetype,cmonth ";
    	this.rs1=stmt.executeQuery(sql);
    	while(rs1.next())
    	{
    	   temp=new TTjIncomingbyarea();
    	   temp.setUpareaname(rs1.getString(2));
    	   temp.setPricetype(rs1.getString(3));
    	   temp.setCmonth(rs1.getString(4));
    	   temp.setIncoming(rs1.getInt(5));  
    	   t.add(temp);
    	}    	
    	this.closeAll();
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally{this.closeAll(); }
        return t;
    }*/
  /*  public List<TTjIncomingbycontent> findIC(String year) {
    	List<TTjIncomingbycontent> t=new ArrayList<TTjIncomingbycontent>();
    	TTjIncomingbycontent temp=null;
    	try {
			this.init();
		
    	this.sql=" select ctype,pricetype,cmonth,sum(incoming) as incoming from t_tj_incomingbycontent" +
    			" where cyear='"+year+"' group by ctype,pricetype,cmonth    order by  ctype,pricetype,cmonth";
    	this.rs1=stmt.executeQuery(sql);
    	while(rs1.next())
    	{
    	   temp=new TTjIncomingbycontent();
    	   temp.setCtype(rs1.getString(1));
    	   temp.setPricetype(rs1.getString(2));
    	   temp.setCmonth(rs1.getString(3));
    	   temp.setIncoming(rs1.getInt(4));  
    	   t.add(temp);
    	}    	
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally{this.closeAll(); }
        return t;
    }*/
   /* public List<TTjSp> findSP(String... year) {
    	List<TTjSp> t=new ArrayList<TTjSp>();
    	TTjSp temp=null;
    	try {
			this.init();
		   switch(year.length)
		   {
		   case 3:
			   this.sql=" select spname,sname,chour,sum(downloadnumber) as dd,sum(ordernumber) as od from t_tj_sp " +
   			            "  where cyear='"+year[0]+"' and cmonth='"+year[1]+"' and cday='"+year[2]+"'  group by spname,sname,chour";
			   break;
		   case 2:
			   this.sql=" select spname,sname,cday,sum(downloadnumber) as dd,sum(ordernumber) as od from t_tj_sp " +
		            "  where cyear='"+year[0]+"' and cmonth='"+year[1]+"'  group by spname,sname,cday";
			   break;
		   case 1:
			   this.sql=" select spname,sname,cmonth,sum(downloadnumber) as dd,sum(ordernumber) as od from t_tj_sp " +
	            "  where cyear='"+year[0]+"'  group by spname,sname,cmonth";		  
			   break;
		   default:
			   break;
		   
		   }
    	    	
    	this.rs1=stmt.executeQuery(sql);
    	while(rs1.next())
    	{
    	   temp=new TTjSp();
    	   temp.setSpname(rs1.getString(1));
    	   temp.setSname(rs1.getString(2));
    	   temp.setCmonth(rs1.getString(3));
    	   temp.setDownloadnumber(rs1.getInt(4));
    	   temp.setOrdernumber(rs1.getInt(5));
    	   t.add(temp);
    	}    	
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally{this.closeAll(); }
        return t;
    }*/
   
    /*public List<TTjMobile> findMobile(String year) {
    	List<TTjMobile> t=new ArrayList<TTjMobile>();
    	TTjMobile temp=null;
    	try {
			this.init();
		
    	this.sql="  select uaname,pricetype,cmonth,sum(downloadnumber) as dd from t_tj_mobile " +
    			" where cyear='"+year+"' group by uaname,pricetype,cmonth order by uaname,pricetype,cmonth ";
    	this.rs1=stmt.executeQuery(sql);
    	while(rs1.next())
    	{
    	   temp=new TTjMobile();
    	   temp.setUaname(rs1.getString(1));
    	   temp.setPricetype(rs1.getString(2));
    	   temp.setCmonth(rs1.getString(3));
    	   temp.setDownloadnumber(rs1.getInt(4));  
    	   t.add(temp);
    	}    	
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally{this.closeAll(); }
        return t;
    }*/
  /*  public List<TTjUsernumber> findUsernumber(String year) {
    	List<TTjUsernumber> t=new ArrayList<TTjUsernumber>();
    	TTjUsernumber temp=null;
    	try {
			this.init();
		
    	this.sql="  select sname,pricetype,cmonth,sum(downloadnumber) as dd from t_tj_usernumber " +
    			" where cyear='"+year+"' group by sname,pricetype,cmonth order by sname,pricetype,cmonth";
    	this.rs1=stmt.executeQuery(sql);
    	while(rs1.next())
    	{
    	   temp=new TTjUsernumber();
    	   temp.setSname(rs1.getString(1));
    	   temp.setPricetype(rs1.getString(2));
    	   temp.setCmonth(rs1.getString(3));
    	   temp.setDownloadnumber(rs1.getInt(4));  
    	   t.add(temp);
    	}    	
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally{this.closeAll(); }
        return t;
    }*/
    /*public List<TTjInoutuser> findIOuser(String... year) {
    	List<TTjInoutuser> t=new ArrayList<TTjInoutuser>();
    	TTjInoutuser temp=null;
    	try {
			this.init();
			
		   switch(year.length)
		   {
		   case 3:
			   this.sql=" select sname,inorout,chour,sum(usernumber) as dd from t_tj_inoutuser " +
   			            "  where cyear='"+year[0]+"' and cmonth='"+year[1]+"' and cday='"+year[2]+"'   group by sname,inorout,chour order by sname,inorout,chour";
			   break;
		   case 2:
			   this.sql=" select sname,inorout,cday,sum(usernumber) as dd from t_tj_inoutuser " +
		            "  where cyear='"+year[0]+"' and cmonth='"+year[1]+"'   group by sname,inorout,cday order by sname,inorout,cday";
			   break;
		   case 1:
			   this.sql=" select sname,inorout,cmonth,sum(usernumber) as dd from t_tj_inoutuser " +
	            "  where cyear='"+year[0]+"'   group by sname,inorout,cmonth order by sname,inorout,cmonth";		  
			   break;
		   default:
			   break;
		   
		   }
    	    	
    	this.rs1=stmt.executeQuery(sql);
    	while(rs1.next())
    	{
    	   temp=new TTjInoutuser();
    	   temp.setSname(rs1.getString(1));
    	   temp.setInorout(rs1.getString(2));
    	   temp.setCmonth(rs1.getString(3));
    	   temp.setUsernumber(rs1.getInt(4));
    	   t.add(temp);
    	}    	
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	finally{this.closeAll(); }
        return t;
    }*/
    
/*    public void save(Person person) {
        if (person.getId() == null) {
            // new
            em.persist(person);
        } else {
            // update
            em.merge(person);
        }
    }

    public void remove(int id) {
        Person person = find(id);
        if (person != null) {
            em.remove(person);
        }
    }
    public Person find(int id) {
        return em.find(Person.class, id);
    }*/
   

   

}